#############################
## Generates Figure A1 ######
#############################

setwd("R:\\ForeignAidandPublicOpinion\\Replication Data")
library(foreign)
library(RItools)

# Create empty matrix
m <- matrix(rep(NA,390),nrow=13,ncol=30)
colnames(m) <- c(1:30)
rownames(m) <- c("Sex","Age","High School", "Liberal", "Moderate", 
			"Conservative", "Income",
			"Hardly Ever Church", "Sometimes Church",
			"At Least Once a Week Church", "Never Lived Outside the US", "Identify w/World as a Whole",
			"Poverty Results from Laziness")
for (i in 0:29) {
# Limit sample to treatment and control
mydata <- read.dta("Exp1Cleaned.dta")
# Redefine treatment groups to facilitate filling matrix
mydata$treatment <- mydata$treatment-1
# Keep only control observations and observations from specified treatment group
mydata <- mydata[mydata$treatment==i | mydata$control==1,]
## Turn categorical variables into dummies
# Politics
mydata$liberal <- mydata$politic==1
mydata$moderate <- mydata$politic==2
mydata$conservative <- mydata$politic==3
# Religion
mydata$religion1 <- mydata$religion==1
mydata$religion2 <- mydata$religion==2
mydata$religion3 <- mydata$religion==3
# World Citizen
mydata$world <- mydata$group==5

## Standardize Variables
mydata$age <- scale(mydata$age)
mydata$sex <- scale(mydata$sex)
mydata$highschool <- scale(mydata$highschool)
mydata$college <- scale(mydata$college)
mydata$liberal <- scale(mydata$liberal)
mydata$moderate <- scale(mydata$moderate)
mydata$conservative <- scale(mydata$conservative)
mydata$income <- scale(mydata$income)
mydata$religion1 <- scale(mydata$religion1)
mydata$religion2 <- scale(mydata$religion2)
mydata$religion3 <- scale(mydata$religion3)
mydata$outside <- scale(mydata$outside)
mydata$world <- scale(mydata$world)
mydata$poverty <- scale(mydata$poverty)

# Fill up matrix with appropriate values
m[13*i+1] <- mean(mydata$sex[mydata$treatment==i])-mean(mydata$sex[mydata$control==1])
m[13*i+2] <- mean(mydata$age[mydata$treatment==i])-mean(mydata$age[mydata$control==1])
m[13*i+3] <- mean(mydata$highschool[mydata$treatment==i])-mean(mydata$highschool[mydata$control==1])
m[13*i+4] <- mean(mydata$liberal[mydata$treatment==i])-mean(mydata$liberal[mydata$control==1])
m[13*i+5] <- mean(mydata$moderate[mydata$treatment==i])-mean(mydata$moderate[mydata$control==1])
m[13*i+6] <- mean(mydata$conservative[mydata$treatment==i])-mean(mydata$conservative[mydata$control==1])
m[13*i+7] <- mean(mydata$income[mydata$treatment==i])-mean(mydata$income[mydata$control==1])
m[13*i+8] <- mean(mydata$religion1[mydata$treatment==i])-mean(mydata$religion1[mydata$control==1])
m[13*i+9] <- mean(mydata$religion2[mydata$treatment==i])-mean(mydata$religion2[mydata$control==1])
m[13*i+10] <- mean(mydata$religion3[mydata$treatment==i])-mean(mydata$religion3[mydata$control==1])
m[13*i+11] <- mean(mydata$outside[mydata$treatment==i])-mean(mydata$outside[mydata$control==1])
m[13*i+12] <- mean(mydata$world[mydata$treatment==i])-mean(mydata$world[mydata$control==1])
m[13*i+13] <- mean(mydata$poverty[mydata$treatment==i])-mean(mydata$poverty[mydata$control==1])
}

dev.off()
balanceplot(m, colors=c(1:30),shapes=c(1:25), main="Balance by Treatment for Experiment 1",
	include.legend = FALSE,xlab="Difference from Control Group (Standard Deviations)")

legend("topright",legend = c(1:30),col=c(1:30),pch=c(1:25),bty="n",title="Treatment",
text.font=.05,cex=.45,
x.intersp = 1, y.intersp = 1,
)
abline(v=.2, col="red")
abline(v=-.2, col="red")


########################
##Generates Figure A2 ##
########################

setwd("R:\\ForeignAidandPublicOpinion\\Replication Data")
library(foreign)
library(RItools)

# Create empty matrix
m <- matrix(rep(NA,156),nrow=13,ncol=12)
colnames(m) <- c(1:12)
rownames(m) <- c("Sex","Age","High School", "Liberal", "Moderate", 
			"Conservative", "Income",
			"Hardly Ever Church", "Sometimes Church",
			"At Least Once a Week Church", "Never Lived Outside the US", "Identify w/World as a Whole",
			"Poverty Results from Laziness")
for (i in 0:11) {
# Limit sample to treatment and control
mydata <- read.dta("Exp2Cleaned.dta")
# Redefine treatment groups to facilitate filling matrix
mydata$treatment <- mydata$treatment-1
# Keep only control observations and observations from specified treatment group
mydata <- mydata[mydata$treatment==i | mydata$control==1,]
## Turn categorical variables into dummies
# Politics
mydata$liberal <- mydata$politic==1
mydata$moderate <- mydata$politic==2
mydata$conservative <- mydata$politic==3
# Religion
mydata$religion1 <- mydata$religion==1
mydata$religion2 <- mydata$religion==2
mydata$religion3 <- mydata$religion==3
# World Citizen
mydata$world <- mydata$group==5

## Standardize Variables
mydata$age <- scale(mydata$age)
mydata$sex <- scale(mydata$sex)
mydata$highschool <- scale(mydata$highschool)
mydata$college <- scale(mydata$college)
mydata$liberal <- scale(mydata$liberal)
mydata$moderate <- scale(mydata$moderate)
mydata$conservative <- scale(mydata$conservative)
mydata$income <- scale(mydata$income)
mydata$religion1 <- scale(mydata$religion1)
mydata$religion2 <- scale(mydata$religion2)
mydata$religion3 <- scale(mydata$religion3)
mydata$outside <- scale(mydata$outside)
mydata$world <- scale(mydata$world)
mydata$poverty <- scale(mydata$poverty)

# Fill up matrix with appropriate values
m[13*i+1] <- mean(mydata$sex[mydata$treatment==i])-mean(mydata$sex[mydata$control==1])
m[13*i+2] <- mean(mydata$age[mydata$treatment==i])-mean(mydata$age[mydata$control==1])
m[13*i+3] <- mean(mydata$highschool[mydata$treatment==i])-mean(mydata$highschool[mydata$control==1])
m[13*i+4] <- mean(mydata$liberal[mydata$treatment==i])-mean(mydata$liberal[mydata$control==1])
m[13*i+5] <- mean(mydata$moderate[mydata$treatment==i])-mean(mydata$moderate[mydata$control==1])
m[13*i+6] <- mean(mydata$conservative[mydata$treatment==i])-mean(mydata$conservative[mydata$control==1])
m[13*i+7] <- mean(mydata$income[mydata$treatment==i])-mean(mydata$income[mydata$control==1])
m[13*i+8] <- mean(mydata$religion1[mydata$treatment==i])-mean(mydata$religion1[mydata$control==1])
m[13*i+9] <- mean(mydata$religion2[mydata$treatment==i])-mean(mydata$religion2[mydata$control==1])
m[13*i+10] <- mean(mydata$religion3[mydata$treatment==i])-mean(mydata$religion3[mydata$control==1])
m[13*i+11] <- mean(mydata$outside[mydata$treatment==i])-mean(mydata$outside[mydata$control==1])
m[13*i+12] <- mean(mydata$world[mydata$treatment==i])-mean(mydata$world[mydata$control==1])
m[13*i+13] <- mean(mydata$poverty[mydata$treatment==i])-mean(mydata$poverty[mydata$control==1])
}

dev.off()
balanceplot(m, colors=c(1:12), main="Balance by Treatment for Experiment 2",
shapes = 1,include.legend = FALSE,xlab="Difference from Control Group (Standard Deviations)")
legend(-.48,27,legend = c(1:12),col=c(1:12),pch=1,bty="n",title="Treatment",
x.intersp = .8, y.intersp = .8,
)
abline(v=.2, col="red")
abline(v=-.2, col="red")


